package com.kyzh.core.viewmodel

import android.net.Uri
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.gushenge.core.beans.Small
import com.gushenge.core.impls.WealImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.MyApplication
import com.kyzh.core.R

class SmallToSellVM : ViewModel() {


    private val small = MutableLiveData<Small>()
    private val images = MutableLiveData<ArrayList<Uri>>()
    private var add = MutableLiveData<Uri>()


    public fun getSmall(): MutableLiveData<Small> {
        return small
    }

    public fun setSmall(small: Small) {
        this.small.value = small
    }


    public fun getImages(): MutableLiveData<ArrayList<Uri>> {
        val uris = images.value
        return if (uris == null) {
            images.value = arrayListOf()
            images
        } else {
            images.value = uris
            images
        }
    }

    /*增加图片*/
    public fun addImage(image: Uri) {
        val uris = getImages().value
        uris?.let {
            it.add(image)
            images.value = it
        }

    }

    public fun deleteImage(image: Uri) {
        val uris = getImages().value
        if (image != add) {
            uris?.let {
                it.remove(image)
                images.value = it
            }
        }
    }


    public fun post(title: String, gPosition:Int,introduction: String, sname: String, twoPassword: String="", images: String, money: String, system: String, listener: ResultListener) {

        if (title.isEmpty() or introduction.isEmpty() or sname.isEmpty() or money.isEmpty() or system.isEmpty() or getImages().value.isNullOrEmpty()) {
            listener.error(MyApplication.instance.getString(R.string.pleaseSelectHaveEmpty))
        } else {
            if (introduction.length < 15) {
                listener.error(MyApplication.instance.getString(R.string.descNo15))
            } else {

                val systemText = if (system == "iOS") 2 else 1
                WealImpl.addDeal(title, introduction, introduction, (small.value?.data?.get(gPosition) as Small.Data)?.gname?:"", sname, (small.value?.data?.get(gPosition) as Small.Data)?.gid?:"", twoPassword?:"", images, small.value!!.uid, money, systemText, object : ResultListener {
                    override fun success(bean: Any) {
                        listener.error(bean.toString())
                        listener.success()
                    }

                    override fun error() {
                        listener.error(MyApplication.instance.getString(R.string.noBindPhone))
                    }

                    override fun error(error: String) {
                        listener.error(error)
                    }
                })
            }
        }
    }
}